package io.geekidea.springbootplus.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.geekidea.springbootplus.framework.common.exception.BusinessException;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.core.pagination.PageInfo;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.shiro.util.SaltUtil;
import io.geekidea.springbootplus.framework.util.PasswordUtil;
import io.geekidea.springbootplus.framework.util.PhoneUtil;
import io.geekidea.springbootplus.system.entity.SysDepartment;
import io.geekidea.springbootplus.system.entity.SysMember;
import io.geekidea.springbootplus.system.entity.SysUser;
import io.geekidea.springbootplus.system.mapper.SysDepartmentMapper;
import io.geekidea.springbootplus.system.mapper.SysMemberMapper;
import io.geekidea.springbootplus.system.param.sysmember.SysMemberPageParam;
import io.geekidea.springbootplus.system.service.SysMemberService;
import io.geekidea.springbootplus.system.vo.SysMemberQueryVo;
import io.geekidea.springbootplus.system.vo.SysUserQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@Slf4j
@Service
public class SysMemberServiceImpl extends BaseServiceImpl<SysMemberMapper, SysMember> implements SysMemberService {

    @Resource
    private SysMemberMapper sysMemberMapper;


    @Override
    public boolean deleteMemberBatchIds(List<Integer> idList) throws Exception {
        int result = sysMemberMapper.deleteMemberByBatch(idList);
        return result > 0;
    }

    @Override
    public Paging<SysMemberQueryVo> getSysMemberPageList(SysMemberPageParam sysMemberPageParam) throws Exception {
        Page<SysMemberQueryVo> page = new PageInfo<>(sysMemberPageParam);
        IPage<SysMemberQueryVo> iPage = sysMemberMapper.getSysMemberPageList(page, sysMemberPageParam);

        // 手机号码脱敏处理
        if (iPage != null && CollectionUtils.isNotEmpty(iPage.getRecords())) {
            iPage.getRecords().forEach(vo -> {
                vo.setPhone(PhoneUtil.desensitize(vo.getPhone()));
            });
        }
        return new Paging(iPage);
    }

    @Override
    public boolean saveSysMember(SysMember sysMember) throws Exception {
        // 校验用户名是否存在
        SysMember member = getMemberByOpenid(sysMember.getOpenid());
        if (member!=null) {
            throw new BusinessException("用户名已存在");
        }
        // 保存系统用户
        return super.save(sysMember);
    }

    @Override
    public SysMember getMemberByOpenid(String openid) throws Exception {
        SysMember sysMember = sysMemberMapper.getSysMemberByOpenid(openid);
        return sysMember;
    }

    @Override
    public boolean deleteSysMember(Integer id) throws Exception {
        Integer result = sysMemberMapper.deleteSysMember(id);
        return result>0;
    }

    @Override
    public SysMemberQueryVo getMemberById(Integer id) throws Exception {
        SysMemberQueryVo sysMember = sysMemberMapper.getSysMemberById(id);
        return sysMember;
    }

    @Override
    public boolean updateSysMember(SysMember sysMember) throws Exception {
        UpdateWrapper<SysMember> uw = new UpdateWrapper<>();
        uw.eq("id",sysMember.getId());
        boolean result = false;
        try {
            result = super.update(sysMember, uw);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

}
